package learn.jvm;

import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import sun.tools.attach.HotSpotVirtualMachine;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Attach {
    public static void main(String[] args) throws IOException, AttachNotSupportedException {
        VirtualMachine attach = VirtualMachine.attach("2028");
        List<VirtualMachineDescriptor> list = VirtualMachine.list();
        InputStream in = ((HotSpotVirtualMachine) attach).remoteDataDump((Object[]) args);
        byte b[] = new byte[256];
        int n = 0;
        do {
            n = in.read(b);
            if (n > 0) {
                System.out.println(new String(b, 0, n));
            }
        } while (n > 0);
        in.close();
        attach.detach();
    }
}
